/**
 * 问卷相关的接口
 * @author 滕轶男
 */

import axios, { ResDataType } from './ajax'

type SearchOption = {
    keyword: string
    isStar: boolean
    isDeleted: boolean
    pageSize: number // 每页多少条
    page: number // 第几页
}

// 获取当个问卷信息
export async function getQuestionServices(id: string): Promise<ResDataType> {
    const url = `/api/question/${id}`
    const data = (await axios.get(url)) as ResDataType
    return data
}

// 创建问卷
export async function createQuestionServices(): Promise<ResDataType> {
    const url = `/api/question`
    const data = (await axios.post(url)) as ResDataType
    return data
}

// 获取（查询）问卷列表
export async function getQuestionListServices(
    // opt: Partial<SearchOption>  代表opt是泛型 SearchOption的一部分
    opt: Partial<SearchOption> = {}
): Promise<ResDataType> {
    const url = `/api/question`
    const data = (await axios.get(url, { params: opt })) as ResDataType

    return data
}

// 更新单个问卷
export async function updateQuestionServices(
    id: string,
    opt: { [key: string]: any }
): Promise<ResDataType> {
    const url = `/api/question/${id}`
    const data = (await axios.patch(url, { ...opt })) as ResDataType
    return data
}

// 复制单个问卷
export async function duplicateQuestionServices(id: string): Promise<ResDataType> {
    const url = `/api/question/duplicate/${id}`
    const data = (await axios.post(url)) as ResDataType
    return data
}

// 批量删除问卷
export async function deleteQuestionListServices(ids: string[]): Promise<ResDataType> {
    const url = `/api/question`
    const data = (await axios.delete(url, { data: { ids } })) as ResDataType
    return data
}
